################################################################################-
# Replication File for Wratil, Wäckerle and Proksch: Government Rhetoric and the 
# Representation of Public Opinion in International Negotiations
#
# This script runs the analysis presented in Appendix I (Sentiment and National 
# Elections). 
#
# Additionally, it produces the following graphs and tables:
# Table L1
################################################################################-

library(tidyverse) #version 1.3.2
library(quanteda)  #version 3.2.1
library(lme4)      #version 1.1-30
library(stargazer) #version 5.2.3

load("generated_data/dfm_for_stm.RData")

data_dfm_main <- data_dfm
docvars(data_dfm_main) <- docvars(data_dfm_main) %>% 
  data.frame() %>% 
  select(gov_eu_supporter,image_lag6m_scaled,date_correct,
         Transcription,distance_elect_planned,final_two_months,
         part_of_speech,text_copy,text_original,Actor,
         gov_lr_cmp_static_scaled,eu_receipts_gdp_scaled,budget_any,unanimity_any,
         unemployment_scaled,inflation_scaled,north_south,
         Negotiation_Stage,Council_Config_final)
data_dfm_main <- dfm_subset(data_dfm_main,complete.cases(docvars(data_dfm_main)))
data.sum <- docvars(data_dfm_main)

sentiment_main <- data_dfm_main %>% 
  dfm_lookup(dictionary = data_dictionary_LSD2015) %>% 
  convert(to="data.frame")

data.sum <- bind_cols(data.sum,sentiment_main %>% 
                        select(positive,negative)) %>% 
  mutate(sentiment = log((positive+0.5)/(negative+0.5))) %>% 
  mutate(distance_elect_planned = distance_elect_planned/365)

mod1 <- lmer(sentiment ~ distance_elect_planned+gov_eu_supporter+image_lag6m_scaled+
               budget_any+unanimity_any+part_of_speech+
               gov_lr_cmp_static_scaled+eu_receipts_gdp_scaled+
               unemployment_scaled+inflation_scaled+north_south+
               Negotiation_Stage+Council_Config_final+
               (1 | Actor)+(1|Transcription), data = data.sum)

mod2 <- lmer(sentiment ~ final_two_months+gov_eu_supporter+image_lag6m_scaled+
               budget_any+unanimity_any+part_of_speech+
               gov_lr_cmp_static_scaled+eu_receipts_gdp_scaled+
               unemployment_scaled+inflation_scaled+north_south+
               Negotiation_Stage+Council_Config_final+
               (1 | Actor)+(1|Transcription), data = data.sum)

stargazer(mod2,mod1,single.row = TRUE,align=TRUE,
                       type="text",
                     intercept.bottom = FALSE)
# This is Table L1
stargazer(mod2,mod1,single.row = TRUE,align=TRUE,
                     #  type="text",
                     dep.var.labels   = "Sentiment",
                     covariate.labels = c("Intercept",
                                          "More Than Two Months Before Election","Distance to Planned Election","Eurosceptic Government",
                                          "Public Image of the EU","Budget issue","Unanimity Required", "Middle Part of Speech",
                                          "End Part of Speech", "Government Left-Right position", 
                                          "Net receipts from EU budget",
                                          "Unemployment Rate","Inflation Rate",
                                          "Northern Europe","Southern Europe","Negotiation stage: Initial Presentation",
                                          "Negotiation stage: Mixed Negotiations","Negotiation stage: Policy Debates",
                                          "Council configuration: Ecofin","Council configuration: EPSCO",
                                          "Council configuration: ENV","Council configuration: JHA"),
                     omit.stat = c("f","ll","ser","aic","bic"),
                     intercept.bottom = FALSE)
